#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<Windows.h>
int high,width;
int bird_x,bird_y;
int bar_y,bar_xTop,bar_xDown;
int score;

void gotoxy(int x,int y)
{
    HANDLE handle=GetStdHandle(STD_OUTPUT_HANDLE);
    COORD pos;
    pos.X=x;
    pos.Y=y;
    SetConsoleCursorPosition(handle,pos);
}

void HideCursor()//用于隐藏光标
{
    CONSOLE_CURSOR_INFO cursor_info={1,0};
    SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
}
void startup()
{
   high=15;
   width=30;
   bird_x=0;
   bird_y=width/3;
   bar_y=width;
   bar_xTop=high/4;
   bar_xDown=high/2+1;
   score=0;

   HideCursor();
}

void show()
{
   gotoxy(0,0);
   int i,j;
   for(i=0;i<=high+1;i++)
   {
       for(j=0;j<=width+1;j++)
       {
           if((i==bird_x)&&(j==bird_y))
            printf("@");
           else if((j==bar_y)&&((i<=bar_xTop)||(i>=bar_xDown)))
            printf("*");
           else
            printf(" ");
       }
       printf("\n");
   }
   printf("Point:%d",score);

}
void updateWithoutInput()
{
    if(bird_y==bar_y)
    {
        if((bird_x>bar_xTop)&&(bird_x<bar_xDown))
            score++;
        else
            exit(0);
    }
    bird_x++;
    if(bar_y>0)
    bar_y--;
    else
    {
        bar_y=width;
        int randPosition=rand()%(high-5);
        bar_xTop=randPosition;
        bar_xDown=randPosition+high/4;
    }

    Sleep(150);
}
void updateWithInput()
{
   char input;
   if(kbhit())
   {
	   input=getch();
	   if(input==' ')
        bird_x=bird_x-2;


   }

}
int main()
{
	startup();
	while(1)
	{
		show();
		updateWithInput();
		updateWithoutInput();
	}
	return 0;
}

